package edu.put.sk.nrpp.ospf.packet.lsa;


import java.net.InetAddress;
import java.net.UnknownHostException;

import edu.put.sk.nrpp.*;

/**
 * Representing LSA Data field (type 1) 
 * Router-LSAs are the Type 1 LSAs.  Each router in an area originates
 *   a router-LSA.  The LSA describes the state and cost of the router's
 *   links (i.e., interfaces) to the area.  All of the router's links to
 *   the area must be described in a single router-LSA.  For details
 *   concerning the construction of router-LSAs, see Section 12.4.1.
 *   <pre>
 *      0                   1                   2                   3
 *      0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
 *      +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
 *0  0  |            LS age             |     Options   |       1       |
 *      +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
 *1  32 |                        Link State ID                          |
 *      +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
 *2  64 |                     Advertising Router                        |
 *      +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
 *3  96 |                     LS sequence number                        |
 *      +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
 *4  128|         LS checksum           |             length            |
 *      +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
 *5  160|    0    |V|E|B|        0      |            # links            |
 *      +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
 *6  192|                          Link ID                              |
 *      +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
 *7  224|                         Link Data                             |
 *      +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
 *8  256|     Type      |     # TOS     |            metric             |
 *      +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
 *9  288|                              ...                              |
 *      +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
 *10 320|      TOS      |        0      |          TOS  metric          |
 *      +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
 *11 352|                          Link ID                              |
 *      +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
 *12 384|                         Link Data                             |
 *      +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
 *13 416|                              ...                              |
 *</pre>
 * description from RFC 2328
 *      
 * 
 * @author kevin
 */

public class LSAType1Data {

	protected Bit[] _data;
	
	
	@Override
	public String toString(){
		
		
		try {
			return "LSAType1Data Type:\t"+getType()+";\tLinkID:"+getLinkID()+"\tMask:\t"+getLinkData();
		} catch (UnknownHostException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
		
	}
	
	public LSAType1Data (Bit[] data) throws Exception {
		if(data.length!=96)
			throw new Exception("Wrong length of data");
		this._data=data;
		
	}
	
	/**
	 *  A quick description of the router link.  One of the following.
        Note that host routes are classified as links to stub networks
        with network mask of 0xffffffff.
		<pre>
        Type   Description
        __________________________________________________
        1      Point-to-point connection to another router
        2      Connection to a transit network
        3      Connection to a stub network
        4      Virtual link
        </pre>
	 * @return
	 */
	public int getType(){
		return Bit.toInt(Bit.cut(_data, 64, 71));
	}
	
	/**
	 *  The number of different TOS metrics given for this link, not
     *   counting the required link metric (referred to as the TOS 0
     *   metric in [Ref9]).  For example, if no additional TOS metrics
     *   are given, this field is set to 0.
	 * @return TOS
	 */
	public int getTOS(){
		return Bit.toInt(Bit.cut(_data, 72, 79));
	}
	/**
	 * The cost of using this router link.
	 * @return cost
	 */
	public int getMetric(){
		return Bit.toInt(Bit.cut(_data, 80, 95));
	}
	
	/**
	 *  Identifies the object that this router link connects to.  Value
        depends on the link's Type.  When connecting to an object that
        also originates an LSA (i.e., another router or a transit
        network) the Link ID is equal to the neighboring LSA's Link
        State ID.  This provides the key for looking up the neighboring
        LSA in the link state database during the routing table
        calculation. See Section 12.2 for more details.

		<pre>
          Type   Link ID
          ______________________________________
          1      Neighboring router's Router ID
          2      IP address of Designated Router
          3      IP network/subnet number
          4      Neighboring router's Router ID
         </pre>
	 * @return Link ID
	 * @throws UnknownHostException if Link ID isn't IP address (or mask)
	 */
	public InetAddress getLinkID() throws UnknownHostException{
		
		return Bit.toInetAddres(Bit.cut(_data, 0, 31));
	}

	public Bit[] getLinkIDByBits() throws UnknownHostException{
		
		return Bit.cut(_data, 0, 31);
	}
	
	/**
	 * Value again depends on the link's Type field. For connections to
        stub networks, Link Data specifies the network's IP address
        mask. For unnumbered point-to-point connections, it specifies
        the interface's MIB-II [Ref8] ifIndex value. For the other link
        types it specifies the router interface's IP address. This
        latter piece of information is needed during the routing table
        build process, when calculating the IP address of the next hop.
        See Section 16.1.1 for more details.
	 * @return Link Data
	 * @throws UnknownHostException if Link Data isn't IP Address or mask
	 */
public InetAddress getLinkData() throws UnknownHostException{
		
		return Bit.toInetAddres(Bit.cut(_data, 32, 63));
	}

public Bit[] getLinkDataBit() throws UnknownHostException{
	
	return Bit.cut(_data, 32, 63);
}
}

